package com.zwb.demo.easyweb.common.entity.converter;

import com.zwb.demo.easyweb.common.entity.ValueEnum;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter(autoApply = true)
public abstract class EnumConverter<E extends ValueEnum<V>, V> implements AttributeConverter<E, V> {

    private final Class<E> clazz;

    public EnumConverter(Class<E> clazz) {
        this.clazz = clazz;
    }

    @Override
    public V convertToDatabaseColumn(E e) {
        return e == null ? null : e.getValue();
    }

    @Override
    public E convertToEntityAttribute(V v) {
        return v == null ? null : ValueEnum.valueToEnum(clazz, v);
    }
}
